﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using Telerik.Web.Mvc.UI;
using Uco.Infrastructure;
using Uco.Models;
using System.Data;

namespace Uco.Areas.Admin.Controllers
{
    [Authorize]
    public class GalleryBannerController : BaseAdminController
    {

        public ActionResult Index()
        {
            return View();
        }

		[GridAction]
        public ActionResult _AjaxIndex()
        {
            return View(new GridModel(
                SF.LoadGalleryBanners().ToList()
            ));
        }

		[AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _AjaxDelete(int ID)
        {
            List<GalleryBanner> l = SF.LoadGalleryBanners();
            l.RemoveAll(r => r.ID == ID);
            SF.SaveGalleryBanners(l);
            SF.CleanTextComponentsRepository();

            CleanCache.CleanOutputCache();

            return View(new GridModel<GalleryBanner>
            {
                Data = SF.LoadGalleryBanners().ToList()
            });
        }


		//NOT AJAX GRID

        public ActionResult Create()
        {
            return View(new GalleryBanner());
        } 

        [HttpPost]
        public ActionResult Create(GalleryBanner obj)
        {
            if (ModelState.IsValid)
            {
                List<GalleryBanner> l = SF.LoadGalleryBanners();
                if (l.Count == 0) obj.ID = 1;
                else obj.ID = l.Max(r => r.ID) + 1;
                l.Add(obj);
                SF.SaveGalleryBanners(l);

                CleanCache.CleanCacheAfterPageEdit();

                return RedirectToAction("Edit", new { ID = obj.ID });
            }
            return View(obj);
        }
        
 
        public ActionResult Edit(int ID)
        {
            List<GalleryBanner> l = SF.LoadGalleryBanners();
            return View(l.First(r => r.ID == ID));
        }

        [HttpPost]
        public ActionResult Edit(int ID, GalleryBanner obj)
        {
            try
            {
                List<GalleryBanner> l = SF.LoadGalleryBanners();
                l.RemoveAll(r => r.ID == ID);
                l.Add(obj);
                SF.SaveGalleryBanners(l);
                SF.CleanTextComponentsRepository();

                CleanCache.CleanOutputCache();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
